package leetcode.backtrace;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class Lc47 {

    boolean[] visited ;
    List<List<Integer>> ans = new ArrayList<>();
    HashMap<Integer, Integer> map = new HashMap<>();

    public List<List<Integer>> permuteUnique(int[] nums) {
        Arrays.sort(nums);
        visited = new boolean[nums.length];
        getpermuteUnique(nums, new ArrayList<Integer>());
        return ans;
    }

    private void getpermuteUnique(int[] nums, List<Integer> res) {
        if (res.size() == nums.length) {
            ans.add(new ArrayList<>(res));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (visited[i] || (i > 0 && nums[i] == nums[i-1] && !visited[i-1])) continue;
            visited[i] = true;
            res.add(nums[i]);
            getpermuteUnique(nums, res);
            res.remove(res.size() - 1);
            visited[i] = false;
        }
    }

    public static void main(String[] args) {
        Lc47 lc47 = new Lc47();
        System.out.println(lc47.permuteUnique(new int[]{1, 1, 2}));

        int[] a = {5,2,1};

        Arrays.sort(a,1,3);
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
    }

}
